import axios, { AxiosInstance } from 'axios'
import { ElNotification, ElLoading } from 'element-plus'
import useStore from '@/store'
import { config as appConfig } from '@/app.config'

export class Http {
    public static request: AxiosInstance
    public static loading: any
    public static shadeNum: number = 0

    public static init() {
        // 创建axios实例
        this.request = axios.create({
            baseURL: 'http://127.0.0.1:3007',  // 本地
            timeout: appConfig.timeout || 10000
        })
        // 初始化拦截器
        this.initInterceptors();
        return axios;
    }

    public static initInterceptors() {
        // 设置post请求头
        this.request.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        /**
         * 请求拦截器
         * 每次请求前，如果存在token则在请求头中携带token
         */
        this.request.interceptors.request.use((config: any) => {
            const token = window.sessionStorage.getItem('token') || ''
            if (token) {
                config.headers.Authorization = 'Bearer ' + token; // 每个请求携带自定义 token
            }
            // 遮罩
            let shade = config.shade === undefined ? true : config.shade;
            if (!!shade) {
                this.loading = ElLoading.service({
                    lock: true,
                    text: 'Loading...',
                    background: 'rgba(255, 255, 255, 0.4)'
                })
                this.shadeNum++
            }
            return config
        }, (error: any) => {
            console.log(error)
        })

         /**
         * 响应拦截器
         */
        this.request.interceptors.response.use((response: any) => {
            // 关闭遮罩
            let shade: any = response.config.shade
            this.closeLoading(shade)
            if (response.status === 200 && response.data && response.data.status == 0) {
                return response;
            } else {
                if(response.data && response.data.status != 0) {
                    this.errorHandle(response.data)
                } else {
                    this.errorHandle(response)
                }
                return response
            }
        }, (error: any) => {
            let shade: any = error.config && error.config.shade
            this.closeLoading(shade)
            const { response } = error
            if (response) {
                if(response.data && response.data.status != 0) {
                    this.errorHandle(response.data)
                } else {
                    this.errorHandle(response)
                }
                return Promise.reject(response.data)
            } else {
                console.log('response', response)
            }
            return response
        })
    }
    /**
     * http握手错误
     * @param response 响应回调
     */
    private static errorHandle(response: any) {
        // 状态码判断
        switch (response.status) {
            case 401:
                console.log('401', response)
                break;
            case 403:
                console.log('403', response)
                break;
            case 404:
                console.log('404', response.data)
                break;
            case 500:
                console.log('500 0001', response)
                break;
            default:
                console.log('连接错误', response.data)
        }
    }

    public static closeLoading(shade: any) {
        shade = shade === undefined ? true : shade
        if (shade) {
            if (this.shadeNum > 0) {
                -- this.shadeNum
            }
            if (this.shadeNum === 0) {
                this.loading.close()
            }
        }
    }
}